//
//  CZEmoticon.swift
//  表情包数据
//
//  Created by 名品导购网MPLife.com on 2017/12/13.
//  Copyright © 2017年 sweet. All rights reserved.
//

import UIKit
import YYModel
class CZEmoticon: NSObject {
    //表情类型 图片表情/true
    @objc var type = false
    //表情字符串 发送给新浪微博的服务器
    @objc var chs:String?
    //表情图片名称
    @objc var png:String?
    @objc var emoji:String?
    //emoji的十六进制编码
    @objc var code:String?{
        didSet{
            guard let codeStr = code else {
                return
            }
            let scanner = Scanner(string: codeStr)
            var result:UInt32 = 0
            scanner.scanHexInt32(&result)
            emoji = String(Character(UnicodeScalar(result)!))
        }
    }
    //表情使用次数
    var times:Int = 0
    
    //表情模型所在目录
    @objc var directory:String?
    @objc var image:UIImage?{
        
            //判断表情类型
            if type {
                return nil
            }
            
            guard let directory = directory,let png = png,let path = Bundle.main.path(forResource: "HMEmoticon.bundle", ofType: nil),let bundle = Bundle(path: path)else {
                return nil
            }
         return UIImage(named: "\(directory)/\(png)", in: bundle, compatibleWith: nil)
        
    }
    
    //将当前的图像转换成图片的属性文本
    func imageText(font:UIFont)->NSAttributedString{
        //判断图像是否存在
        guard let image = image else {
            return NSAttributedString(string: "")
        }
        //创建文本附件
    
        let attachment = CZEmoticonAttachment()
        //记录属性文本文字
        attachment.chs = chs
        attachment.image = image
        let height = font.lineHeight
        attachment.bounds = CGRect(x: 0, y: -4, width: height, height: height)
        
        let attStrm = NSMutableAttributedString(attributedString:NSAttributedString(attachment: attachment))
        attStrm.addAttributes([NSAttributedStringKey.font : font], range: NSRange(location: 0, length: 1))
        //返回属性文本
        return  attStrm
    }
        
    override var description: String{
        return yy_modelDescription()
    }
}













